#ifndef _STRTOL_H_
#define _STRTOL_H_

#include <errno.h>

/* Convert a string to a long integer.
 * Assumes that the upper and lower case
 * alphabets and digits are each contiguous.
 */
long
zstrtol_l(const char *str, int base)
{
  const char *s;
  char c;
  int sign;
  long acc = 0;
  
  s = str;
  c = *s++;
  if (c == '-') {
    c = *s++;
    sign = 1;
  } else {
    sign = 0;
    if (c == '+') 
      c = *s++;
  }
  if ((base == 0 || base == 16) &&
      c == '0' && ( *s == 'x' || *s == 'X')) {
    base = 16;
    c = s[1];
    s += 2;
  }
  if (base == 0)
    base = c == '0' ? 8 : 10;
  if (base < 2 || base > 36)
    return EINVAL;
  for (;; c = *s++) {
    if (c >= '0' && c <= '9') {
      c = c - '0';
    }
    else if (c >= 'a' && c <= 'f') {
      c = c - 'a' + 10;
    } 
    else if (c >= 'A' && c <= 'F') {
      c = c - 'A' + 10;
    }
    else
      break;
    
    if (c > base)
      break;   
    acc = acc * base + c;
  }
  if (sign)
    acc = -acc;
  return acc;
}


#endif /* _STRTOL_H_ */

